package org.myself.meeting.controller;

import org.myself.meeting.model.*;
import org.myself.meeting.service.ChatsService;
import org.myself.meeting.service.DepartmentService;
import org.myself.meeting.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
public class ChatsController {
    @Autowired
    DepartmentService departmentService;

    @Autowired
    EmployeeService employeeService;

    @Autowired
    ChatsService chatsService;

    //聊天界面
    @RequestMapping("/chat")
    @ResponseBody
    public Result chat(@RequestBody Base base){
        Employee emp =employeeService.getEmpsByUsername(base.getUsername()) ;
        Map<String,Object> map = new HashMap<String, Object>();
        //查询
        map.put("chats",chatsService.getAllChat());
        map.put("username",emp.getEmployeename());
        map.put("dapartment",departmentService.getDepById(emp.getDepartmentid()).getDepartmentname());
        map.put("otherpeople",employeeService.getAllEmpsByWithDep());
        return new Result(200,"查询成功",map);
    }

    //进入聊天数据库中
    @RequestMapping("/chatContent")
    @ResponseBody
    public Result chatContent(@RequestBody ChatContentSaveAcc acc){
        if (acc.getContent().length() == 0){
            return new Result(400,"请输入要发表的消息",null);
        }
        Employee emp =employeeService.getEmpsByUsername(acc.getUsername()) ;
        //组装
        Chats chats = new Chats();
        chats.setEmployeename(emp.getEmployeename());
        chats.setContent(acc.getContent());
        chats.setDepartmentname(departmentService.getDepById(emp.getDepartmentid()).getDepartmentname());
        chats.setSpeaktime(new Date());
        chatsService.addChats(chats);
        System.out.println("插入数据完成!");
        return new Result(200,"插入成功",null);
    }
    @RequestMapping("/chathis")
    @ResponseBody
    public Result chathis(@RequestBody Chathis chathis){
        Employee emp =employeeService.getEmpsByUsername(chathis.getUsername()) ;
        Map<String,Object> map = new HashMap<String, Object>();
        //查询
        map.put("chats",chatsService.getAllChat(chathis.getPage()));
        map.put("username",emp.getEmployeename());
        map.put("dapartment",departmentService.getDepById(emp.getDepartmentid()).getDepartmentname());
        map.put("otherpeople",employeeService.getAllEmpsByWithDep());
        return new Result(200,"查询成功",map);
    }
    //视频通话
    @RequestMapping("/chatbyvideo")
    public String chatByVideo(HttpSession session,Model model){
        Employee employee = (Employee)session.getAttribute("currentuser");
        model.addAttribute("id",employee.getEmployeeid());
        return "chatbyvideo";
    }

}
